﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using ElfTyping.General;
using ElfTyping.Animations;
using ElfTyping.States.GameState.Effects;

namespace ElfTyping.States.GameState.Entities
{
    public enum EnemyDifficulty
    {
        Easy,
        Medium,
        Hard
    }

    public abstract class Enemy : WordEntity
    {
        public int Damage { get; protected set; }
        public int Experience { get; protected set; }
        public bool IsActive { get; protected set; }
        public int BaseSpeed { get; protected set; }
        public int MoveSpeed
        {
            get { return (int)(BaseSpeed * SpeedMultiplier + 0.0001f); }
        }

        public float SpeedMultiplier { get; set; } 

        public Animation DeathAnimation { get; private set; }
        protected Animation MovingAnimation { get; private set; }
        protected Animation AttackingAnimation { get; private set; }

        protected static Random random = new Random();

        public Enemy(GameScript script, Vector2 center, EnemyDifficulty difficulty)
            : base(script, center)
        {
            IsActive = true;
            SpeedMultiplier = 1f;
        }

        protected void LoadAnimations(string imageFile)
        {
            DeathAnimation = new FireEmblemNormalStanceAnimation(imageFile);
            MovingAnimation = new FireEmblemMovingStanceAnimation(imageFile);
            //MovingAnimation = new ChestAnimation();
            AttackingAnimation = new FireEmblemFocusStanceAnimation(imageFile);

            CurrentAnimation = MovingAnimation;
        }

        public override void Draw(General.Camera camera)
        {
            if (IsActive)
            {
                UntypedLetterColor = Color.Black;

                //camera.MakeText(Speed.ToString())
                //    .WithColor(Color.Blue)
                //    .WithCenteredPosition(CenterPosition.X, CenterPosition.Y - DrawBox.Height * 1.1f)
                //    .Draw();
            }
            else
            {
                UntypedLetterColor = Color.SlateGray;
                TypedLetterColor = Color.SlateGray;
            }

            base.Draw(camera);
        }

        protected override void OnWordFinish()
        {
            Script.AddEnemyToAttack(this);

            base.OnWordFinish();
        }

        

        /// <summary>
        /// Tells the enemy that the next letter has been pressed.
        /// </summary>
        /// <returns>Whether the word has been finished.</returns>
        public override AcceptLetterResult AcceptLetter()
        {
            if (!IsActive)
            {
                return AcceptLetterResult.Failed;
            }

            return base.AcceptLetter();
        }

        protected override void Die()
        {
            Script.EnemyDeath(this);

            int rand = random.Next(1, 100);
            if (rand <= Experience)
            { // you win an item!
                Script.DropRandomPuzzlePiece(this);
                Script.SpawnEffect(new ChestEffect(this), true);
            }

            base.Die();
        }

        public virtual void DoAttack()
        {
            IsActive = false;
            // Do animations or whatever for attacking
        }
    }
}
